package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    private static final String ALL = "__all__";
    private static final String DYNA = "__dyna__";

    /**
     * 首页信息 分页查询,根据publishTime倒序拿去10条
     *
     * @param articleHomeDto
     * @return {@link List}<{@link ApArticle}>
     */
    @Override
    public List<ApArticle> load(ArticleHomeDto articleHomeDto) {
        Page<ApArticle> page = new Page<>(1,articleHomeDto.getSize());
        ApArticle apArticle = new ApArticle();
        if (!StringUtils.equalsAny(articleHomeDto.getTag(), ALL, DYNA)) {
            apArticle.setChannelId(Integer.valueOf(articleHomeDto.getTag()));
        }
        //涉及到多表查询,所以需要自定义查询 ap_article,ap_article_config
        List<ApArticle> apArticleList =  baseMapper.selectPageLimit(page,apArticle);
        return apArticleList;
    }

    /**
     * 加载更多
     *
     * @param articleHomeDto
     * @return {@link List}<{@link ApArticle}>
     */
    @Override
    public List<ApArticle> loadmore(ArticleHomeDto articleHomeDto) {
        Page<ApArticle> page = new Page<>(1,articleHomeDto.getSize());
        ApArticle apArticle = new ApArticle();
        if (!StringUtils.equalsAny(articleHomeDto.getTag(),ALL,DYNA)) {
            apArticle.setChannelId(Integer.valueOf(articleHomeDto.getTag()));
        }
        apArticle.setPublishTime(articleHomeDto.getMinBehotTime());
        List<ApArticle> apArticleList =  baseMapper.selectLoadMorePageLimit(page,apArticle);
        return apArticleList;
    }

    /**
     * 加载新文章
     *
     * @param articleHomeDto
     * @return {@link List}<{@link ApArticle}>
     */
    @Override
    public List<ApArticle> loadnew(ArticleHomeDto articleHomeDto) {
        Page<ApArticle> page = new Page<>(1,articleHomeDto.getSize());
        ApArticle apArticle = new ApArticle();
        if (!StringUtils.equalsAny(articleHomeDto.getTag(),ALL,DYNA)) {
            apArticle.setChannelId(Integer.valueOf(articleHomeDto.getTag()));
        }
        apArticle.setPublishTime(articleHomeDto.getMaxBehotTime());
        List<ApArticle> apArticleList =  baseMapper.selectLoadNewPageLimit(page,apArticle);
        return apArticleList;
    }
}
